<?php namespace Common\Model;
use \Hdphp\Model\Model;
/*
 * 标签模型
 * */
	class Tag extends Model
	{
		protected $table = "tag";//数据表
			//自动验证
		protected $validate = array(
			array("tname","required","标签名不为空",3,3),
		);
		/*
		 * 首页查询列表
		 * */
		 public function liebiao()
		 {
		 	//查询所有数据
		 	return $this->orderBy("tid","asc")->get();
		 }
		/*
		 * 添加
		 * */
		 public function add_Tag()
		 {
		 	
			//p($_POST);die;
		 	if(!$this->create()) return false;
			//判断是标签名是否存在
			$Tagexist = $this->where("tname",$_POST["tname"])->first();
			//p($Tagexist);die;
			if(!empty($Tagexist)){
				$this->error ="该标签已存在";
				return false;
			}
			//处理数据
			$str = $_POST['tname'];
			//p($str);die;
			$tname = explode("|",$str);
			//p($Tagarr);die;
			foreach($tname as $k=>$v){
				//$this->add(array('tname'=>$v));
				$this->add(array("tname"=>$v));
			}
			return TRUE;
			
		 }
		 /*
		  * 编辑
		  * */
		 public function reset_Tag()
		 {
		 	if(!$this->create()) return false;
			$Tagexist = $this->where("tname",$_POST["tname"])->first();
			//p($Tagexist);die;
			if(!empty($Tagexist)){
				$this->error ="该标签已存在";
				return false;
			}
			//执行更新
			return $this->save();
		 }
		 /*
		  * 删除
		  * */
		  public function del_Tag($tid)
		  {
		  	$data = Db::table("article_tag")->where("tag_tid",$tid)->get();
			//p($data);die;
			if(empty($data)){
				$this->where("tid",$tid)->delete();
				return TRUE;
			}else{
				$this->error = "删除失败";
				return false;
			}
		  	
		  }
	}
	